<!-- 账户加款 -->
<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { type FormInstance, type FormRules, ElMessage } from "element-plus"
import { getUserDataApi, addMoneyS } from "@/api/user"
import { usePagination } from "@/hooks/usePagination"
import { useUserStore } from "@/store/modules/user"
import { checkNumberZF } from "@/utils/validate"
const userStore = useUserStore()
const { paginationData } = usePagination()
const loading = ref<boolean>(false)
const ruleFormRef = ref<FormInstance>()
// 账户列表 用户名列表
// #region
const tableData = ref<any[]>([])
const searchData = reactive({
  name: "",
  team: "",
  nickName: ""
})
const tableDataA = ref<any[]>([])
const getTableData = () => {
  loading.value = true
  getUserDataApi({
    page: 1,
    size: 100000,
    name: searchData.name || undefined,
    department: "",
    team: "",
    role: "",
    maxOrder: userStore.usermaxOrder
    // phone: searchData.phone || undefined
  })
    .then((res) => {
      paginationData.total = res.data.total
      tableData.value = res.data.userList
      tableDataA.value = tableData.value.map((item) => {
        return item.name
      })
      console.log(tableDataA.value, "遍历后的账号名")
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
getTableData()
const rules = reactive<FormRules>({
  money: [{ required: true, message: "请输入充值金额" }, { validator: checkNumberZF }],
  team: [{ required: true, message: "请选择账号", trigger: "change" }]
})
//  #endregion
const ruleForm = reactive({
  money: "", // 充值金额
  remark: "", // 充值备注
  team: "" // 充值团队
})
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      addMoneyS({
        name: ruleForm.team,
        remark: ruleForm.remark,
        num: ruleForm.money
      }).then((res) => {
        ElMessage.success("充值成功")
        ruleForm.money = ""
        ruleForm.team = ""
        formEl.resetFields()
        console.log(res, "123456")
      })
      console.log(ruleForm.money)
    } else {
      console.log("error submit!", fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

// 短信充值
import { SendingAdd } from "@/api/SendingRecord/index"
const formRef = ref<FormInstance | null>(null)
const formData = reactive<any>({ data: {} }) // 回显示赋值
const SendingRecordCZ = () => {
  formData.data.money = ""
  SendingAdd(formData.data).then((res: any) => {
    console.log(res, "充值后的数据")
    if (res.code == 200) {
      ElMessage.success(res.data)
      formData.data = {}
    }
  })
}
const clearValidate = () => {
  formRef.value?.clearValidate()
}
const clearForm = () => {
  formData.data = {}
  clearValidate()
}

// #region 查找账号
import { useUserListStore } from "@/store/modules/userList"
const UserListArr = ref()
const UserStore = useUserListStore()
UserStore.getUserListFn()
watch(
  [() => UserStore.userListStore],
  (newValue) => {
    UserListArr.value = newValue.flat()
  },
  {
    immediate: true
  }
)
// #endregion
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <template #header>
        <div class="card-header">
          <span>账号充值</span>
        </div>
      </template>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        style="max-width: 300px"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="充值金额" prop="money">
          <el-input v-model="ruleForm.money" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item prop="team" label="充值账号">
          <el-select v-model="ruleForm.team" clearable placeholder="请选择充值账号" style="width: 240px" filterable>
            <el-option v-for="item in tableDataA" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="充值备注" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"> 充值 </el-button>
          <el-button @click="resetForm(ruleFormRef)">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <template #header>
        <div class="card-header">
          <span>短信充值</span>
        </div>
      </template>
      <el-form ref="formRef" :model="formData.data" label-width="100px" label-position="left">
        <el-form-item prop="id" label="充值账号">
          <el-select v-model="formData.data.id" clearable placeholder="请选择充值账号" style="width: 240px" filterable>
            <el-option v-for="item in UserListArr" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="充值条数" prop="smsRows">
          <el-input v-model="formData.data.smsRows" placeholder="请输入充值条数" style="width: 240px" />
        </el-form-item>
        <div>
          <el-text class="mx-1" type="info">充值规则: 70字内(含70字)计一条,超过70字,按67字/条计费。</el-text>
          <br />
        </div>
      </el-form>
      <br />
      <el-form-item>
        <el-button type="primary" @click="SendingRecordCZ">充值</el-button>
        <el-button @click="clearForm">清空</el-button>
      </el-form-item>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
}
</style>
